﻿using StackExchange.Profiling;

namespace Framework.WebApi
{
	public class ToMiniProfiler
	{
		/// <summary>
		/// 判断是否启用 MiniProfiler
		/// </summary>
		/// <returns></returns>
		internal static bool CanBeMiniProfiler()
		{
			
			//// 减少不必要的监听
			//if (App.HttpContext == null
			//	) return false;

			return true;
		}

		/// <summary>
		/// 打印验证信息到 MiniProfiler
		/// </summary>
		/// <param name="category">分类</param>
		/// <param name="state">状态</param>
		/// <param name="message">消息</param>
		/// <param name="isError">是否为警告消息</param>
		public static void PrintToMiniProfiler(string category, string state, string message = null, bool isError = false)
		{
			if (!CanBeMiniProfiler()) return;

			// 打印消息
			var titleCaseCategory = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(category);
			var customTiming = MiniProfiler.Current?.CustomTiming(category, string.IsNullOrWhiteSpace(message) ? $"{titleCaseCategory} {state}" : message, state);
			if (customTiming == null) return;

			// 判断是否是警告消息
			if (isError) customTiming.Errored = true;
		}
	}
}
